package com.duduli.li;

import com.duduli.li.mapper.JdMapper;
import com.duduli.li.po.Product;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.Reader;
import java.util.ArrayList;
import java.util.List;

public class InsertProduct {
    public void insertProduct() throws IOException {
        List<String> list = new ArrayList<String>();
        //笔记本
        for(int i = 1;i <= 827; i++){
//            白酒
            String s = "https://list.jd.com/list.html?cat=12259,12260,9435&page="+i+"&sort=sort_rank_asc&trans=1&JL=6_0_0&ms=6#J_main";
//            手机
//            String s = "https://list.jd.com/list.html?cat=9987,653,655&page="+i+"&sort=sort_rank_asc&trans=1&JL=6_0_0&ms=6#J_main";
//            电脑
//            String s = "https://list.jd.com/list.html?cat=670,671,672&page="+i+"&sort=sort%5Ftotalsales15%5Fdesc&trans=1&JL=6_0_0&ms=6#J_main";
            list.add(s);
        }
        Reader reader = Resources.getResourceAsReader("config/conf.xml");
        SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(reader);
        SqlSession sqlSession = factory.openSession();
        JdMapper mapper =sqlSession.getMapper(JdMapper.class);
        GetWebProduct g = new GetWebProduct();
        for(String s:list){
           for(Product p : g.getProduct(s)){
//               System.out.println(p.toString());
               System.out.println(p.getId());
               System.out.println(p.getName());
               System.out.println(p.getCategory());
               mapper.insertProduct(p);

               System.out.println("--------------");
           }

        }
        sqlSession.commit();
        sqlSession.close();
    }

    public static void main(String[] args) throws IOException {
        InsertProduct ip = new InsertProduct();
        ip.insertProduct();
    }
}
